package vn.demand.dragon.view
{
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.formatters.DateFormatter;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.ReferenceProxy;
	import vn.demand.dragon.view.components.AccountingProjectSearch;
	import vn.demand.utils.DpUtils;
	
	public class AccountingProjectSearchMediator extends Mediator
	{
		public static const NAME:String = "AccountingProjectSearchMediator";
		
		private var masterdataProxy:MasterdataProxy;
		private var projectProxy:ReferenceProxy;
		
		private var masterdataLoaded:Boolean = false;
		private var viewCreated:Boolean = false;
		
		public function AccountingProjectSearchMediator(mediatorId:String, view:Object)
		{
			super(mediatorId, view);
			
			masterdataProxy = facade.retrieveProxy(MasterdataProxy.NAME) as MasterdataProxy;
			projectProxy = facade.retrieveProxy(ReferenceProxy.NAME) as ReferenceProxy;
			
			searchForm.addEventListener('viewCreated', onViewCreated);
			searchForm.addEventListener('onSearch', onSearch);
			searchForm.addEventListener('onReset', onReset);
		}
		
		public function onViewCreated(event:Event):void {
			viewCreated = true;
			if (masterdataLoaded) {
				prepareData();
			}
		}
		 
		public function prepareData():void {
			searchForm.p_customer.dataProvider = masterdataProxy.customersDP;
			searchForm.p_status.dataProvider = masterdataProxy.projectStatusDP;
			
			searchForm.p_customer.selectedIndex = 0;
			searchForm.p_status.selectedData = 10;
			
//			searchForm.p_status.enabled = false;
		} 
		
		 private function doValidate():Boolean {
        	var errors:String = "";
        	if (DpUtils.compare(searchForm.p_from.selectedDate, searchForm.p_to.selectedDate) < 0) {
				errors += "Valid From date must before Valid To date\n";
			}
        	if (DpUtils.compare(searchForm.p_complete_from.selectedDate, searchForm.p_complete_to.selectedDate) < 0) {
				errors += "Valid Complete From date must before Valid Complete To date\n";
			}
			if (errors.length > 0) {
				alert.error(errors);
				return false;
			}
        	return true;
        }
		
		public function onSearch(event:Event):void {
			if (doValidate()) {
				var o:Object = {};
				//o.project_rstatus = searchForm.p_status.selectedData;
				o.project_company = searchForm.p_customer.selectedData;
				
				var df:DateFormatter = CommonCommand.dateFormatter;
	            o.from_date = df.format(searchForm.p_from.selectedDate);
	            o.to_date = df.format(searchForm.p_from.selectedDate);
	            o.complete_from_date = df.format(searchForm.p_complete_from.selectedDate);
	            o.complete_to_date = df.format(searchForm.p_complete_to.selectedDate);
	            
	            o.project_iso_code = searchForm.p_code.text;
	            
	            sendNotification(CommonCommand.ON_SEARCH_REFERENCES, o);
	            projectProxy.loadReferences(o);
			}
		}
		
		public function onReset(event:Event):void {
			searchForm.p_customer.selectedIndex = 0;
			searchForm.p_status.selectedData = 10;
			searchForm.p_code.text = '';
			searchForm.p_from.data = null;
			searchForm.p_to.data = null;
			searchForm.p_complete_from.data = null;
			searchForm.p_complete_to.data = null;
			
			onSearch(new Event('onSearch'));
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ FFIAApplicationFacade.LOAD_MASTERDATA_SUCCESS,
            		FFIAApplicationFacade.TASK_UPDATED,
            		FFIAApplicationFacade.TASK_LOG_UPDATED   
            ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
				case FFIAApplicationFacade.LOAD_MASTERDATA_SUCCESS:
					masterdataLoaded = true;
					onViewCreated(null);
					break;
					//update lai danh sach reference khi update Task
				case FFIAApplicationFacade.TASK_UPDATED:
					projectProxy.loadReferences();
					break;
					//update lai danh sach reference khi update Task Log
				case FFIAApplicationFacade.TASK_LOG_UPDATED:
					projectProxy.loadReferences({});
					break;
			}
        }
		
		public function get searchForm():AccountingProjectSearch {
			return viewComponent as AccountingProjectSearch;
		}

	}
}